package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.Account;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.AccountMapper;
import com.kgc.basicprovider.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {

    private Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public ResponseMsg selectAccountByCode(String code) {
        logger.info("AccountServiceImpl selectAccountByCode code:{}", code);
        ResponseMsg msg = new ResponseMsg();
        Account account = accountMapper.selectAccountByCode(code);
        if (account != null) {
            msg.setCode("301");
            msg.setMsg("客户编码不能重复！");
            return msg;
        }
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMsg selectAccountByPage(Map<String, Object> map) {
        logger.info("AccountServiceImpl selectAccountByPage params,{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<Account> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize(), "create_date desc");
        List<Account> accountList = accountMapper.selectAccountByPage(map);
        pageInfo.setList(accountList);
        pageInfo.setTotal((int) page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("AccountServiceImpl selectAccountByPage result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertAccount(Account account) {
        logger.info("AccountServiceImpl insertAccount params,{}", account);
        ResponseMsg responseMsg = new ResponseMsg();
        account.setCreateDate(new Date());
        account.setUpdateDate(new Date());

        int result = accountMapper.insertAccount(account);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("AccountServiceImpl insertAccount result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateAccount(Account account) {
        logger.info("AccountServiceImpl updateAccount params,{}", account);
        ResponseMsg responseMsg = new ResponseMsg();
        int result = accountMapper.updateAccount(account);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("AccountServiceImpl updateAccount result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteAccount(int id) {
        logger.info("AccountServiceImpl deleteAccount params,{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        int result = accountMapper.deleteAccount(id);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("AccountServiceImpl deleteAccount result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectAllAccount() {
        Map<String,Object> map = new HashMap<>();
        List<Account> accountList = accountMapper.selectAccountByPage(map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("200");
        responseMsg.setData(accountList);
        logger.debug("AccountServiceImpl selectAllAccount result:{}", responseMsg);
        return responseMsg;
    }
}
